from beanie import Document
from starlette.authentication import SimpleUser


class User(Document):
    username: str

    class Config:
        schema_extra = {
            "example": {
                "username": "用户名"
            }
        }


class UserIn(User):
    password: str

    class Config:
        schema_extra = {
            "example": {
                "username": "用户名",
                "password": "密码"
            }
        }


class UserPermission(User):
    path: str
    method: str

    class Config:
        schema_extra = {
            "example": {
                "username": "用户名",
                "path": "权限路径",
                "method": "权限操作"
            }
        }


class UserRole(User):
    role_id: str

    class Config:
        schema_extra = {
            "example": {
                "username": "用户名",
                "role_id": "角色标识"
            }
        }


class JWTUser(SimpleUser):
    def __init__(self, username: str, token: str, payload: dict) -> None:
        super(JWTUser, self).__init__(username)
        self.token = token
        self.payload = payload

    @property
    def identity(self) -> str:
        return self.username
